package ru.st.selenium.pages;

import static org.openqa.selenium.support.ui.ExpectedConditions.*;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

public class FilmPage extends AnyPage {

	public FilmPage(PageManager pages) {
		super(pages);
		
	}
	
	public FilmPage ensurePageLoaded() {
	      super.ensurePageLoaded();
	      wait.until(presenceOfElementLocated(By.id("movie")));
	      return this;
	    }
		
	@FindBy(css = "img[alt=\"Add movie\"]")
	  private WebElement addMovieBtn;
	  
	@FindBy(name = "name")
	  private WebElement filmTitleField;
	
	@FindBy(name = "year")
	  private WebElement filmYearField;
	
	@FindBy(id = "submit")
	  private WebElement submitBtn;
	
	@FindBy(className = "movie_box")
	  private List<WebElement> filmsList;
	
	@FindBy(css = "img[alt=\"Remove\"]")
	  private WebElement removeBtn;
	
	@FindBy(css = "div.content")
	  private WebElement noMoviesWereFound;
	
	public void clickAddMovieBtn() {
		addMovieBtn.click();	  
	  }
	
	public FilmPage setFilmTitle(String text) {
		filmTitleField.sendKeys(text);
		return this;
	}
	
	public FilmPage setFilmYear(String text) {
		filmYearField.sendKeys(text);
		return this;
	}

	public void clickSubmitBtn() {
	    submitBtn.click();
	}
	
	public void clickFirstFilm() {
		filmsList.get(0).click();
		
	}
	
	public FilmPage clickRemoveBtn() {
		removeBtn.click();
		wait.until(alertIsPresent()).accept();
		return pages.filmPage;  
	}
	
	public boolean nothingFound( ){
		return noMoviesWereFound.getText().matches("No movies where found.");
	}
}
